//
//  ALPS_ClassifyClickinController.m
//  Alps
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_ClassifyClickinController.h"
#import "ALPS_leftTableCellModel.h"

#import "ALPS_RightTableCellModel.h"
#import "ALPS_ClassifyRightTableCell.h"

// 专辑界面
#import "ALPS_ClassifyClickClickAlbumController.h"

@interface ALPS_ClassifyClickinController ()<UITableViewDelegate, UITableViewDataSource>

/** 左侧索引栏数据源 */
@property (nonatomic, strong) NSMutableArray *leftScrollDaraSourceArr;
/** 左侧索引栏 */
@property (nonatomic, strong) UITableView *leftTableView;
/** 右侧内容table数据源 */
@property (nonatomic, strong) NSMutableArray *rightTableDataSourceArr;
/** 右侧内容table */
@property (nonatomic, strong) UITableView *rightTableView;
/** 左侧table当前页监控 */
@property (nonatomic, assign) NSInteger pageNum;
/** model */
@property (nonatomic, strong) ALPS_leftTableCellModel *leftCellModel;
/** 记录push界面的 */
@property (nonatomic, strong) NSMutableArray *pushTempArr;

@end

@implementation ALPS_ClassifyClickinController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.000];
    
    NSLog(@"点进来这页的id--- %ld", self.id);
    
    self.leftScrollDaraSourceArr = [NSMutableArray array];
    self.rightTableDataSourceArr = [NSMutableArray array];
    self.pushTempArr = [NSMutableArray array];
    self.pageNum = 1;
    
    // 获取左侧顶部索引栏数据
    [self getDataForLeftScroll];
    
    // 创建左table
    [self createLeftTableView];
    
    // 创建右table
    [self createRightTableView];
    
    [self setLeftBackButton];
    
}

#pragma mark -- 获取左侧顶部索引栏数据
- (void)getDataForLeftScroll
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v3/category/recommends?categoryId=%ld&contentType=album&device=iPhone&scale=2&statEvent=pageview%%2Fcategory%%40有声书&statModule=有声书&statPage=tab%%40发现_分类&version=4.3.80", self.id] body:nil headers:nil response:WSSJSON success:^(id result) {
        NSLog(@"%ld", self.id);
        NSDictionary *tempDic = result[@"keywords"];
        NSArray *arr = tempDic[@"list"];
        for (NSDictionary *dic in arr)
        {
            ALPS_leftTableCellModel *model = [[ALPS_leftTableCellModel alloc] initWithDic:dic];
            [self.leftScrollDaraSourceArr addObject:model];
        }
        if (self.leftScrollDaraSourceArr.count > 0)
        {
            [self.leftTableView reloadData];
            [self getDataForRightTableWithpageNumber:1 indexPathRow:0];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error.description);
    }];
}

#pragma mark -- 创建左侧索引table
- (void)createLeftTableView
{
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth * 0.28, ScreeHeight - 64)];
    [self.view addSubview:self.leftTableView];
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 用系统的注册
    [self.leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"table"];
}

#pragma mark -- 左右table代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.leftTableView)
    {
        return self.leftScrollDaraSourceArr.count;
    }
    else
    {
        return self.rightTableDataSourceArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"table"];
        ALPS_leftTableCellModel *model = [self.leftScrollDaraSourceArr objectAtIndex:indexPath.row];
        cell.textLabel.text = model.keywordName;
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.textLabel.textAlignment = 2;
        cell.textLabel.numberOfLines = 0;
        return cell;
    }
    else
    {
        ALPS_ClassifyRightTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ALPS_ClassifyRightTableCell"];
        ALPS_RightTableCellModel *model = [self.rightTableDataSourceArr objectAtIndex:indexPath.row];
        cell.model = model;
        return cell;
    }

}

#pragma mark -- 左右table点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView)
    {
        ALPS_leftTableCellModel *model = [self.leftScrollDaraSourceArr objectAtIndex:indexPath.row];
        // 获取右table数据
        [self.rightTableDataSourceArr removeAllObjects];
        [self.rightTableView reloadData];
        [self getDataForRightTableWithpageNumber:self.pageNum indexPathRow:indexPath.row];

        NSLog(@"keywordId = %ld", model.keywordId);
    }
    else
    {
        ALPS_ClassifyClickClickAlbumController *AlbumVC = [[ALPS_ClassifyClickClickAlbumController alloc] init];
        NSDictionary *tempDic = [self.pushTempArr objectAtIndex:indexPath.row];
        
        AlbumVC.albumId = [tempDic[@"albumId"] integerValue];
        AlbumVC.id = [tempDic[@"id"] integerValue];
        AlbumVC.coverLarge = tempDic[@"coverLarge"];
        AlbumVC.title = tempDic[@"title"];
        AlbumVC.playsCounts = [tempDic[@"playsCounts"] integerValue];
        AlbumVC.tracks = [tempDic[@"tracks"] integerValue];
        NSLog(@"专辑页albumId = %ld", AlbumVC.albumId);
        [self.navigationController pushViewController:AlbumVC animated:YES];
    }
}

#pragma mark -- 右侧table的数据请求
- (void)getDataForRightTableWithpageNumber:(NSInteger)pageNumber
                             indexPathRow:(NSInteger)indexPathRow
{
    ALPS_leftTableCellModel *model = [self.leftScrollDaraSourceArr objectAtIndex:indexPathRow];
    self.leftCellModel = model;
    
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/keyword/albums?calcDimension=hot&categoryId=%ld&device=iPhone&keywordId=%ld&pageId=%ld&pageSize=20&statEvent=pageview/category@%@&statModule=%@&statPage=tab@发现_分类&status=0", self.id, model.keywordId, pageNumber, model.keywordName, model.keywordName];
    NSLog(@"++++++   %@", urlString);
    
    [WSSNetTool GetNetByURL:urlString body:nil headers:nil response:WSSJSON success:^(id result) {
        
        NSArray *arr = result[@"list"];
        // 给推出的赋值
        self.pushTempArr = [NSMutableArray arrayWithArray:arr];
        
        for (NSDictionary *dic in arr)
        {
            ALPS_RightTableCellModel *model = [[ALPS_RightTableCellModel alloc] initWithDic:dic];
            [self.rightTableDataSourceArr addObject:model];
        }
        if (self.rightTableDataSourceArr.count > 0)
        {
            [self.rightTableView reloadData];
             self.rightTableView.contentOffset = CGPointMake(0, 0);
        }
        [self.rightTableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error.description);
    }];
}

#pragma mark -- 右侧table的创建
- (void)createRightTableView
{
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(ScreeWidth * 0.28, 0, ScreeWidth * 0.72, ScreeHeight - 64)];
    [self.view addSubview:_rightTableView];
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.rowHeight = 90;
    self.rightTableView.scrollsToTop = YES;

    // 注册
    [self.rightTableView registerClass:[ALPS_ClassifyRightTableCell class] forCellReuseIdentifier:@"ALPS_ClassifyRightTableCell"];
    
    //上拉加载
    self.rightTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self upRefreshForGetMoreData];
    }];
}

#pragma mark -- 上拉加载
- (void)upRefreshForGetMoreData
{
    self.pageNum++;
    
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/keyword/albums?calcDimension=hot&categoryId=%ld&device=iPhone&keywordId=%ld&pageId=%ld&pageSize=20&statEvent=pageview/category@%@&statModule=%@&statPage=tab@发现_分类&status=0", self.id, self.leftCellModel.keywordId, self.pageNum, self.leftCellModel.keywordName, self.leftCellModel.keywordName];
    
    [WSSNetTool GetNetByURL:urlString body:nil headers:nil response:WSSJSON success:^(id result) {
        
        NSArray *arr = result[@"list"];

        for (NSDictionary *dic in arr)
        {
            [self.pushTempArr addObject:dic];
            
            ALPS_RightTableCellModel *model = [[ALPS_RightTableCellModel alloc] initWithDic:dic];
            [self.rightTableDataSourceArr addObject:model];
        }
        [self.rightTableView reloadData];
        [self.rightTableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
